package _02_链表;


import java.util.HashMap;
import java.util.Map;

// Random pointer of node with label 13 points to a node from the original list.
public class _剑指_Offer_35_复杂链表的复制 {

    // 7 7
    public Node copyRandomList(Node head) {
        Map<Node, Node> nodes = new HashMap<>();
        Node temp = head;
        // 记录
        while (temp != null) {
            nodes.put(temp, new Node(temp.val));
            temp = temp.next;
        }
        temp = head;
        while (temp != null) {
            Node curNode = nodes.get(temp);
            curNode.next = nodes.get(temp.next);
            curNode.random = nodes.get(temp.random);
            temp = temp.next;
        }
        return nodes.get(head);
    }

}
